package com.dwyinyuan.infrastructure.config;

import com.dwyinyuan.infrastructure.util.interceptor.*;
import lombok.extern.slf4j.*;
import org.springframework.context.annotation.*;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.*;


/**
 * 微信公众号：笛舞音缘
 * <p>
 * Web配置类
 * <p>
 * Created by andy  on 2020-12-16.
 */
@Configuration
@EnableWebMvc
@Slf4j
public class WebConfig implements WebMvcConfigurer {

    /**
     * 全局限流拦截器
     */
    @Resource
    private RateLimitInterceptor rateLimitInterceptor;

    /**
     * 向Web中添加拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 配置限流拦截器，拦截所有以/api/开头的请求
        registry.addInterceptor(rateLimitInterceptor)
                .addPathPatterns("/api/**");
    }

    /**
     * 静态资源配置
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        // 配置本地文件夹目录映射
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:/Users/if/Desktop/MOOC/" +
                        "04-告别996系列" +
                        "/百度网盘/版本管理代码/all-learning/uploads/");

        // Swagger2做的映射
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
